// 树转列表
List treeToArray(List tree) {
  return tree.fold([], (List con, item) {
    con.add(item);
    if (item['children'] != null && item['children'] is List && item['children'].isNotEmpty) {
      con.addAll(treeToArray(item['children']));
    }
    return con;
  });
}

// 列表转树
List arrayToTree(List list, String idKey, {String parentKey = 'parentId'}) {
  final map = {};
  // 创建节点映射
  for (final item in list) {
    map[item[idKey]] = {...item, "children": null};
  }
  // 构建树结构
  final List result = [];
  for (final item in list) {
    if (item[parentKey] == 0 || item[parentKey] == null) {
      result.add(map[item[idKey]]);
    } else {
      map[item[parentKey]]?['children'] ??= [];
      map[item[parentKey]]?['children']?.add(map[item[idKey]]);
    }
  }
  return result;
}
